package com.ijunfu.mail;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 *
 * @Title  : 
 * @Author : ijunfu <ijunfu@163.com>
 * @Date   : 2023/11/30 16:20
 * @Version: 1.0
 * @Motto  : 世界很大 世界很小
 *
 */
@Slf4j
@Service
public class MailService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.username:0}")
    private String from;

    public void sendTextEmail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);

        mailSender.send(message);
        log.info("邮件发送成功");
    }

    public void sendHtmlEmail(String to, String subject, String body) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, false);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(body, true);

        mailSender.send(mimeMessage);
        log.info("邮件发送成功");
    }

    public void sendAttachmentsEmail(String to, String subject, String body, File... files) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(body, true);

        Arrays.stream(files).forEach(file -> {
            try {
                messageHelper.addAttachment(file.getName(), file);
            } catch (MessagingException e) {
                log.error("添加附件失败");;
                throw new RuntimeException(e);
            }
        });

        mailSender.send(mimeMessage);
        log.info("邮件发送成功");
    }

    public void sendInlineResourceEmail(String to, String subject, String body, String rscPath, String rscId) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(body, true);

        if (rscPath!= null && rscId!= null) {
            messageHelper.addInline(rscId, new File(rscPath));
        }

        mailSender.send(mimeMessage);

        log.info("邮件发送成功");
    }
}
